<?php
/**
 * log class
 * @static
 * @uses Magnum_Utility_Stack_Manager
 */
class Magnum_Log extends Magnum_Utility_Stack_Manager
{
    /**
     * the time pointer
     * array so one per group
     * @var array
     */
    static protected $_t = array();


    /**
     * the duration storage
     * array so one per group
     * @var array
     */
    static protected $_d = array();


    // start or not?
    static protected $_on = false;

    /**
     * instance
     * @var Magnum_Log
     */
    static protected $_this = null;


    /**
     * if not called, don't start
     */
    static function start()
    {
        if (defined('_LOG')) {
            self::$_on = _LOG;

            // get instance...
            if (!self::$_this instanceof Magnum_Log) {
                // always a single instance...
                self::$_this = new Magnum_Log;
            }
        }

    }// end start


    // is it on?
    static function on()
    {
        return self::$_on;

    }// end on


    /**
     * some logging of what's happening!
     * @param str $message the emssage body
     * @param str $group to group the message
     * @param str $class class name
     * @param str $func the function name
     * @param array $params function parameters
     * @param bool $isError if true, error!
     * @return void
     */
    static function _log($message, $group = 'default', $details = array('class' => 'N/A', 'func' => 'N/A', 'params' => ''), $isError = false)
    {
        if (!self::$_on) return false;

        // set counter
        $duration = self::counter($group);

        if ($isError) {
            $message = '<span class="error">' . $message . '</span>';
        }

        $logItem = array(
            'message' => $message,
            'class' => $details['class'],
            'function' => $details['func'],
            'parameters' => $details['params'],
            'duration' => $duration,
            'isError' => $isError
        );

        self::$_this->get($group, 1)->set($logItem);

    }// end _log


    /**
     * counter for seconds..
     * @param str $group the group
     * @return mixed
     */
    static public function counter($group = 'default')
    {
        if (!self::$_on) return false;
        // first, make time
        $microtime = microtime(1);

        // set to previous group...
        $curTime = self::getMicrotime($group);

        if ($curTime == 0) {
            $duration = 0;
        } else {
            $duration = number_format($microtime - $curTime, 4);
        }

        self::$_t[$group] = $microtime;
        self::$_d[$group] = $duration;

        // still, return the duration...
        return $duration;

    }// end counter


    /**
     * get duration
     * @param str $group group as the key
     * @param bool $set if true, set as well.
     * @return float
     */
    static public function getMicrotime($group = 'default', $set = true)
    {
        if (!self::$_on) return false;
        // run
        if (!isset(self::$_t[$group])) {
            if ($set) {
                self::$_t[$group] = 0;
            }
            return 0;
        }

        return self::$_t[$group];

    }// end getMicrotime


    /**
     * get duration
     * @param str $group group as the key
     * @return float
     */
    static public function getDuration($group = 'default')
    {
        if (!self::$_on) return false;

        if (!isset(self::$_d[$group])) {
            return 0;
        }

        return self::$_d[$group];

    }// end getDuration


    /**
     * report it
     */
    static function _report()
    {
        if (!self::$_on) return false;

        $logs = self::$_this->getStack();
        require_once Magnum::dir('APPCORE') . 'views/log.php';

    }// end _report

}// end Magnum_log

// short hand:
function __log($message, $group = 'default', $details = array('class' => 'N/A', 'func' => 'N/A', 'params' => ''), $isError = false)
{
    return Magnum_Log::_log($message, $group, $details, $isError);

}// end _log
?>